package Controller;

import DataAccess.DataAccessFacade;
import DomainModel.Course;
import DomainModel.Role;
import DomainModel.User;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.json.JSONArray;
import org.json.JSONObject;

/**
 *
 * @author Mershack
 */
public class ViewCourseController extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession(); 
        try {                      
            //Get the courses and redirect to the admin_view_course or the view_course page
              DataAccessFacade daf = DataAccessFacade.getInstance();
              
             User u = (User)session.getAttribute("currentUser");
             
          /*   if(u==null){
                 //redirect to error page no valid session
             }
             else{*/
                 
                 //re-fetch the updated courses for the database
                 
                  /* get the courses of the user */
                 ArrayList <Course> courses = new ArrayList<Course>();
                 
                 /*if(u.getRole() == Role.ADMIN){//if admin get all courses                     
                     courses = daf.getAllCourses();                                  
                     //response.sendRedirect("admin_view_courses.jsp");
                 }
                 else{//get only the user specific courses                     
                     courses = daf.getUserCourses(u.getUserId());                    
                     //response.sendRedirect("view_courses.jsp");
                 }  */    
                 
                 /*testing with these sample values*/
                 courses.add(new Course(1,"AAA", "Java I", 3));
                 courses.add(new Course(2,"BBB", "Java II", 3));
                 courses.add(new Course(3,"CCC", "C++", 3));
                 courses.add(new Course(4,"DDD", "Art. Int.", 3));
                 
                JSONArray coursesJson = new JSONArray();
                
                for (Course course : courses) {                    
                    JSONObject courseJson = new JSONObject();     
                    
                    courseJson.put("courseID", course.getCourseId());
                    courseJson.put("CourseName", course.getCourseName());
                    courseJson.put("CreditHours", course.getCreditHours());
                                       
                    coursesJson.put(courseJson);
                    
                }
                
                String coursesStr = coursesJson.toString();
                
                out.print(coursesStr);               
                 
            // }
                            
        } catch (Exception e) {
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
